package com.example.qrcodelibrary.QRCodeHelper;


import com.bjw.bean.ComBean;
import com.bjw.utils.SerialHelper;
import com.example.baselibrary.Utils.LogUtil;
import com.example.qrcodelibrary.QRCodeInterface.QRGetMessageInterface;
import com.example.qrcodelibrary.Utils.ErrorCodeEnum;
import com.example.qrcodelibrary.Utils.ThrowErrorException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android_serialport_api.SerialPortFinder;


//二维码 和  三杆闸机 帮助工具
public class QRCodeHelper {
    private static boolean isPreper = false;
    private static SerialHelper serialHelper;
    private static SerialHelper serialHelpergate;
    private static SerialPortFinder serialPortFinder;
    private static List<String> listPoint = new ArrayList<>();
    private static QRGetMessageInterface qRgetMessage;
    private static QRGetMessageInterface GategetMessage;

    public static boolean preperQRFinder() {
        serialPortFinder = new SerialPortFinder();
        if (null != serialPortFinder) {
            preperSerial();
            isPreper = true;
            return isPreper;
        } else {
            return false;
        }
    }

    private static void preperSerial() {
        //二维码 串口对象
        serialHelper = new SerialHelper() {
            @Override
            protected void onDataReceived(ComBean comBean) {
                //将数据通过接口信息回调到外面。
                qRgetMessage.getQRMessage(comBean);
            }
        };

        //三杆机 串口对象
        serialHelpergate = new SerialHelper() {
            @Override
            protected void onDataReceived(ComBean comBean) {
                //将数据通过接口信息回调到外面。
                qRgetMessage.getGateMessage(comBean);
//                GategetMessage.getGateMessage(comBean);
            }
        };

    }

    //打开单个VGA串口
    public static void linkSerial(String point, String bote) {
        serialHelper.close();
        boolean isPoint = serialHelper.setPort(point);
        boolean isBaud = serialHelper.setBaudRate(bote);
        if (isPoint && isBaud) {
            try {
                serialHelper.open();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //打开单个VGA串口
    public static void linkSerialGate(String point, String bote) {
        serialHelpergate.close();
        boolean isPoint = serialHelpergate.setPort(point);
        boolean isBaud = serialHelpergate.setBaudRate(bote);
        if (isPoint && isBaud) {
            try {
                serialHelpergate.open();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String> getDevicePorts() {
        if (isPreper) {
            String[] ports = serialPortFinder.getAllDevicesPath();
            for (int i = 0; i < ports.length; i++) {
                listPoint.add(ports[i]);
            }
        } else {
            //没有初始化，先去初始化serialPortFinder
            throw new ThrowErrorException(ErrorCodeEnum.SERIAL_PORT_NOT_INITIALIZED);
        }
        return listPoint;
    }

    //发送三杆 命令
    public static void sendGateMessage(String s) {
        if (null == s) {
            return;
        }
        try {
            serialHelpergate.sendHex(s);
//            LogUtil.v("发送三杆 命令  持续发送:----" + s);
        }catch (Exception e){
            LogUtil.v("发送三杆 命令:---- 失败" + s);
        }
    }

    //关闭 串口
    public static void CloseSerial() {
        if (serialHelper != null) {
            serialHelper.close();
        }
        if (serialHelpergate != null) {
            serialHelpergate.close();
        }
    }

    public static void setQRGetMessageListener(QRGetMessageInterface qrGetMessageListener) {
        qRgetMessage = qrGetMessageListener;
    }

//    public static void setGateGetMessageListener(QRGetMessageInterface qrGatetMessageListener) {
//        GategetMessage = qrGatetMessageListener;
//    }


}
